//
//  WSDiscoverItemController.m
//  CharonTVProject
//
//  Created by karong on 2023/7/31.
//

#import "WSDiscoverItemController.h"
#import "WSDiscoverRequest.h"
#import "WSChannel.h"
#import "WSChannelItem.h"
#import "WSBannerCell.h"
#import "WSBroadcastCell.H"
#import "WScharonTV_ChannelHeaderView.h"
#import "WSChannelVideoCell.h"
#import "WSChannelRankCell.h"
#import "CTMediator+WSDiscoverAction.h"
#import "CTMediator+WSPlayerAction.h"
#import "CTMediator+WSUserCenterAction.h"
#import "AppDelegate.h"

@interface WSDiscoverItemController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, WScharonTV_ChannelHeaderViewDelegate, WSBannercharonTV_CellDelegate, WSChannelRankCellDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (nonatomic, strong) NSArray * bannerList;

@end

@implementation WSDiscoverItemController

- (void)viewDidLoad {
    [super viewDidLoad];
    WSDiscoverRequest * request = [WSDiscoverRequest new];
    request.charonTV_channelId = self.channel.channel_Id;
    request.charonTV_channelType = self.channel.charonTV_channel_type;
    self.request = request;
}

- (void)showBookDetailWithBook:(id)book {
    
}
- (void)refreshData {
    [self loadcharonTV_BannerRequest];
    [super refreshData];
}

- (void)loadcharonTV_BannerRequest {
    [WSDiscoverRequest loadcharonTV_ChannelListWithChannelId:self.channel.channel_Id channelType:self.channel.charonTV_channel_type success:^(NSArray *charonTV_array) {
        self.bannerList = charonTV_array;
        [self.listView reloadData];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        
    }];
}
#pragma mark RefreshResultProtocol
- (void)loadSuccessWithArray:(NSArray *)array isRefresh:(BOOL)isRefresh {
    [super loadSuccessWithArray:array isRefresh:isRefresh];
}

- (BOOL)disableAutoBeginRefresh {
    return YES;
}

#pragma mark UICollectionDelegte
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.bannerList.count + MIN(self.dataArray.count, 1);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger count = 0;
    if (section < self.bannerList.count) {
        WSChannelItem * charonTV_channelItem = self.bannerList[section];
        count = charonTV_channelItem.row;
    }
    else {
        count = self.dataArray.count;
    }
    return count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell * cell;
    if (indexPath.section < self.bannerList.count) {
        WSChannelItem * charonTV_channelItem = self.bannerList[indexPath.section];
        NSString * cellClass = charonTV_channelItem.cellClass;
        if ([cellClass isEqualToString:@"WSBannerCell"]) {
            WSBannerCell * bannerCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WSBannerCell" forIndexPath:indexPath];
            bannerCell.delegate = self;
            [bannerCell setDataWithObject:charonTV_channelItem.items];
            cell = bannerCell;
        }
        else if ([cellClass isEqualToString:@"WSBroadcastCell"]) {
            WSBroadcastCell * broadcastCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WSBroadcastCell" forIndexPath:indexPath];
            [broadcastCell setDataWithObject:charonTV_channelItem.items];
            cell = broadcastCell;
        }
    }
    else {
        WSVideo * video = self.dataArray[indexPath.row];
        if (video.list.count > 0) {
            WSChannelRankCell * channelRankCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WSChannelRankCell" forIndexPath:indexPath];
            channelRankCell.delegate = self;
            [channelRankCell setDataWithObject:video cellSize:video.cellSize];
            cell = channelRankCell;
        }
        else {
            WSChannelVideoCell * channelVideoCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WSChannelVideoCell" forIndexPath:indexPath];
            [channelVideoCell setDataWithObject:video];
            cell = channelVideoCell;
        }
    }
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    if (indexPath.section < self.bannerList.count) {
        WSChannelItem * charonTV_channelItem = self.bannerList[indexPath.section];
        size = charonTV_channelItem.cellSize;
    }
    else {
        WSVideo * video = self.dataArray[indexPath.row];
        size = video.cellSize;
    }
    
    return size;
}



- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    UIEdgeInsets inset = UIEdgeInsetsZero;
    inset.left = 16;
    inset.right = 16;
    if (section < self.bannerList.count) {
        WSChannelItem * charonTV_channelItem = self.bannerList[section];
        inset = charonTV_channelItem.inset;
    }
    if (section == 0) {
        inset.top = 15;
    }
    return inset;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 16;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section < self.bannerList.count) {
        
    }
    else {
        WSVideo * video = self.dataArray[indexPath.row];
        UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_PlayerWithVideo:video];
        [self.navigationController pushViewController:target animated:YES];
    }
    
}
#pragma mark WSBannercharonTV_CellDelegate
- (void)bannercharonTV_CellAction:(BaseCollectionViewCell *)cell object:(WSItem *)object {
    switch (object.jump_type) {
        case 1:
        {
            UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_PurchaseVCWithIndex:0];
            [self.navigationController pushViewController:target animated:YES];
        }
            break;
        case 2:
        {
            UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_PurchaseVCWithIndex:1];
            [self.navigationController pushViewController:target animated:YES];
        }
            break;
        case 3:
        {
            WSVideo * video = [WSVideo new];
            video.tv_id = object.tv_id;
            UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_PlayerWithVideo:video];
            [self.navigationController pushViewController:target animated:YES];
        }
            break;
        case 12:
        {
            if (appUserCenter.isLogin) {
                UIViewController *userInfoVC = [[CTMediator sharedInstance]ct_showcharonTV_UserInfoWithUser:appUserCenter];
                [self.navigationController pushViewController:userInfoVC animated:YES];
            }
            else {
                [((AppDelegate*)([UIApplication sharedApplication].delegate)) initMainPage];
            }
        }
            break;
        case 13:
        {
            [self.tabBarController setSelectedIndex:2];
        }
            break;
        default:
            break;
    }
}

#pragma mark WSChannelRankCellDelegate
- (void)channelRankCellMoreAtion:(WSVideo *)object {
    UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_ChannelMoreWithSort_type:object.sort_type];
    [self.navigationController pushViewController:target animated:YES];
}

- (void)channelRankCellDidSelectAtion:(id)object {
    UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_PlayerWithVideo:object];
    [self.navigationController pushViewController:target animated:YES];
}
@end
